//
//  ST_PeculiarityAgeNeighborFilterController.swift
//  btvideo
//
//  Created by mac on 2020/9/10.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import RealmSwift
import SwifterSwift



class ST_PeculiarityAgeNeighborFilterController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.height.height.equalTo(324+16+BTSafeAreaBottom)
        }
        
        view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
            make.top.equalToSuperview()
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        root_view.addSubview(reset_btn)
        reset_btn.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(titleLabel)
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(44)
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom+34-54)
        }
       
        root_view.addSubview(neighborItem)
        neighborItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(ageItem)
        ageItem.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(neighborItem.snp.bottom).offset(5)
            make.height.equalTo(92)
        }
        
        bing_default_filter()
    }
    
    var current_filter = ST_PinterestHomeFilterModel()
    
    func bing_default_filter()  {
        
       
        
        ageItem.bind(option: self.current_filter.age)
        
        neighborItem.bind(model: current_filter.neighbor)
        
    }
    
//    MARK: - Properties
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var top_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.clear
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(close_click_handle))
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "搜索条件"
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var reset_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("清除选择")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.setTitleColorForAllStates(UIColor.init(hexString: "#999999")!)
        control.addTarget(self, action: #selector(reset_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var root_scroll_view:UIScrollView = {
        let control = UIScrollView()
//        control.backgroundColor = Color.random
        return control
    }()
    
    fileprivate lazy var contentView:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    
    
    fileprivate lazy var confirm_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("确定")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 22
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    
    fileprivate lazy var ageItem:ST_FilterRangeSliderItemView = {
        
        let control = ST_FilterRangeSliderItemView()
        control.didChange = {[weak self] min,max in
           
            self?.current_filter.age.minValue = min
            self?.current_filter.age.maxValue = max
            
            self?.bing_default_filter()
        }
        return control
    }()
    
    fileprivate lazy var neighborItem:PinterestFilterSwitchControl = {
        
        let control = PinterestFilterSwitchControl()
        control.pushOptionClosure = {[weak self] value in
           
            self?.current_filter.neighbor = value
            
            self?.bing_default_filter()
            
        }
        return control
    }()
    
    
//    MARK: - Event
    @objc func close_click_handle(){
        
        goback_previous_controller()
        
    }
    
    @objc func reset_handle(){
        
        self.current_filter = ST_PinterestHomeFilterModel()
        
        bing_default_filter()
    }
    
    var refresh_closure:(([String:Any])->())?
    
    @objc func confirm_handle(){
        
        
        refresh_closure?(self.current_filter.conver_to_param())
        goback_previous_controller()
        
    }
    
}
